//
//  HPBaseSwiper.h
//  HPZZTong
//
//  Created by wenjun on 14-5-7.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "HPSwiperResult.h"

/**
 刷卡器连接状态
 **/
typedef enum {
    SwiperStateLowBattery       = -4, //电量低
    SwiperStateConnectCanceled  = -3,// 用户取消
    SwiperStateNoDeviceDected   = -2, // 已断开
    SwiperStateConnectFailed    = -1, // 连接失败
    SwiperStateDectedDevice     = 0, // 已检测设备
    SwiperStateConnecting       = 1, // 正在连接
    SwiperStateConnected        = 2, // 已插入设备
} SwiperState;

/**
 密钥类型
 **/
typedef enum {
    EncyptTypeNone = 0,         // 原始
    EncyptTypeFastTransfer,     //快速转账（用银联密钥)
    EncyptTypeNormal,           //其他操作
}EncyptType;

typedef void (^SwiperComplete)(HPSwiperResult * swiperResult);

typedef HPSwiperResult * (^OperationBlock)(void);

#define ExecuteSwiperBlock(block,result) if(block){block(result);}

@protocol HPSwiperFunction <NSObject>

@optional

// 获取KSN
- (void)getKSN:(SwiperComplete)complete;

// 关键数据加密
- (void)encryptKeyData:(NSString *)keyData factor:(NSString *)factor complete:(SwiperComplete)complete;

// mac加密
- (void)encryptMac:(NSString *)macStr factor:(NSString *)factor complete:(SwiperComplete)complete;

- (void)encryptPin:(NSString *)macStr factor:(NSString *)factor complete:(SwiperComplete)complete;

- (void)encyptAmount:(NSString *)amount factor:(NSString *)factor complete:(SwiperComplete)complete;

// 刷卡
- (void)swipe:(int)time factor:(NSString *)factor complete:(SwiperComplete)complete;

- (void)emv:(id)emvInfo complete:(SwiperComplete)complete;

// 密码输入
- (void)inputPwd:(int)time amount:(NSString *)amount factor:(NSString *)factor complete:(SwiperComplete)complete;
-(void)selectApp:(NSArray*)type time:(int)time factor:(NSString *)factor complete:(SwiperComplete)complete;

@end

@interface HPBaseSwiper : NSObject <HPSwiperFunction>

@property (nonatomic) NSTimeInterval defaultTimeout;

@property (strong,nonatomic) NSString * ksn;

@property (readonly,nonatomic) SwiperState connectState;

@property (readonly,nonatomic) dispatch_queue_t swiperQueue;

@property (readonly,nonatomic) dispatch_queue_t wrapperQueue;

+ (id)sharedInstance;

- (id)initWithSwiperQueue:(dispatch_queue_t)swiperQueue wrapperQueue:(dispatch_queue_t)wrapperQueue;

- (void)connectStateChanged:(SwiperState)connectState;

//调用同步方法,OperationBlock返回nil表示异步返回结果,需要调用operationFinishedWithResult结束
- (void)doOperation:(OperationBlock)operation complete:(SwiperComplete)complete;
//调用异步方法
- (void)beginSwiperOperation:(NSInvocation *)invocation timeout:(NSTimeInterval)timeout complete:(SwiperComplete)complete;
//异步方法结束
- (void)operationFinishedWithResult:(HPSwiperResult *)swiperResult;
//停止刷卡器操作
- (void)stopSwiper;

- (BOOL)isConnected;

- (BOOL)hasOperation;

- (BOOL)isBlueThooth;

- (BOOL)canInputPin;

- (void)disconnect;
//取消当前操作
- (void)cancelSwiperOperation;

- (void)cancelSwiperOperationWithCode:(HPSwiperResultCode)resultCode;

- (void)cancelSwiper;

- (void)sleep:(NSTimeInterval)time;

- (void)stopOperation;

- (void)startOperation;

- (BOOL)checkIsICCard:(NSDictionary*)dic;

@end

//// 确认金额
//- (void)affirmAmount:(NSString *)amount factor:(NSString *)factor complete:(SwiperComplete)complete;
// 选择密钥区目录
//- (void)selectDir:(int)type complete:(SwiperComplete)complete;